package com.example.service;

import cn.hutool.core.util.StrUtil;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.exception.CustomerException;
import com.example.mapper.AdminMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {
    @Autowired
    private AdminMapper adminMapper;
    public List<Admin> selectAll(Admin admin){
        return adminMapper.selectAll(admin);
    }

    public PageInfo<Admin> selectPage(Integer pageNum, Integer pageSize,Admin admin) {
        PageHelper.startPage(pageNum,pageSize);
        List<Admin> list = adminMapper.selectAll(admin);
        return PageInfo.of(list);

    }

    public void add(Admin admin) {
       Admin admin1= adminMapper.selectByUsername(admin.getUsername());
       if(admin1!=null){
           throw new CustomerException("账号重复");
       }
       if(StrUtil.isBlank(admin.getPassword())){
           admin.setPassword("123456");
       }
       admin.setRole("ADMIN");
        adminMapper.insert(admin);
    }

    public void update(Admin admin) {
        adminMapper.updateById(admin);
    }

    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }

    public void deleteBatch(List<Admin> list) {
        for (Admin admin : list) {
            adminMapper.deleteById(admin.getId());
        }
    }

    public Admin login(Account account) {
        Admin admin1 = adminMapper.selectByUsername(account.getUsername());
        if(admin1==null){
            throw new CustomerException("账号或密码错误");
        }
        if(!admin1.getPassword().equals(account.getPassword())){
            throw new CustomerException("账号或密码错误");
        }
        String token = TokenUtils.createToken(admin1.getId() + "-" + "ADMIN", admin1.getPassword());
        admin1.setToken(token);
        return admin1;
    }

    public Admin selectById(String id) {
        return adminMapper.selectById(id);
    }

    public void updatePassword(Account account) {
        if(!account.getNewPassword().equals(account.getNew2Password())){
            throw new CustomerException("500","您两次输入的密码不一致");
        }
        Account currentUser=TokenUtils.getCurrentUser();
        if(!account.getPassword().equals(currentUser.getPassword())){
            throw new CustomerException("500","原密码错误");
        }
        Admin admin = adminMapper.selectById(currentUser.getId().toString());
        admin.setPassword(account.getNewPassword());
        adminMapper.updateById(admin);
    }
}
